<?php
class OrderAction extends BaseAction{
	// 首页
	public function index(){
		$pagedata = $this->searchOrder();
		$this->assign('pagedata', $pagedata);
		$this->assignRequestParams();
		$this->display();
	}
	
	public function searchOrder($limit = 20){
		$search_orderId 		= $_REQUEST['search_orderId'];
		$search_shelfId 		= $_REQUEST['search_shelfId'];
		$search_sendermobile 	= $_REQUEST['search_sendermobile'];
		$search_ispay 			= $_REQUEST['search_ispay'];
		$search_partner_id 		= $_REQUEST['search_partner_id'];
		$search_goods_id 		= $_REQUEST['search_goods_id'];
		$search_enable 			= $_REQUEST['search_enable'];
		//dump($_REQUEST);
		if(!empty($search_orderId)){
			$where['id']=$search_orderId;
		}
		if(!empty($search_sendermobile)){
			$where['sender_mobile']=$search_sendermobile;
		}
		if(empty($search_shelfId)){
			if(!empty($search_partner_id)){
				$shelfids = M('ViewShelf')->where("partner_id='".$search_partner_id."'")->field('shelf_id')->findAll();
				$shelfids = getSubByKey($shelfids, "shelf_id");
				//dump($shelfids);
				$where['shelf_id']=array('in',$shelfids);
			}
			if(!empty($search_goods_id)){
				$shelfids = M('ViewShelf')->where("goods_id='".$search_goods_id."'")->field('shelf_id')->findAll();
				$shelfids = getSubByKey($shelfids, "shelf_id");
				$where['shelf_id']=array('in',$shelfids);
			}
		}else{
			$where['shelf_id']=array('in',array($search_shelfId));
		}
		if($search_ispay=='pay'){
			$where['pay_state']=$search_ispay;
		}
		if($search_ispay=='unpay'){
			$where['pay_state']=$search_ispay;
		}
		if($search_enable=='Y'){
			$where['enable']=$search_enable;
		}
		if($search_enable=='N'){
			$where['enable']=$search_enable;
		}
		$order='pay_time desc,ctime desc';
		$orderdao = M('Order');
		$pagedata = $orderdao->kPageQuery($where,$order,$limit);
		//echo $orderdao->getLastSql();
		return $pagedata;
	}
	
	public function coupon(){
		$search_orderId 		= $_REQUEST['search_orderId'];
		$search_passcode 		= $_REQUEST['search_passcode'];
		$search_receiver_mobile = $_REQUEST['search_receiver_mobile'];
		$search_is_send 		= $_REQUEST['search_is_send'];
		$search_is_active 		= $_REQUEST['search_is_active'];
		$search_is_used 		= $_REQUEST['search_is_used'];
		$search_is_expired		= $_REQUEST['search_is_expired'];
		//dump($_REQUEST);
		if(!empty($search_orderId)){
			$where['order_id']=$search_orderId;
		}
		if(!empty($search_passcode)){
			$where['passcode']=$search_passcode;
		}
		if(!empty($search_receiver_mobile)){
			$where['receiver_mobile']=$search_receiver_mobile;
		}
		
		
		if($search_is_send=='Y'||$search_is_send=='N'||$search_is_send=='E'){
			$where['is_send']=$search_is_send;
		}
		
		
		if($search_is_active=='Y'||$search_is_active=='N'){
			$where['is_active']=$search_is_active;
		}
		
		if($search_is_expired=='N'){
			$where['expired_date']=array('gt',time());
		}
		if($search_is_expired=='Y'){
			//$where['expired_date']=array('gt',time());
			//$where['expired_date']=array('NEQ',0);
			$where['expired_date']=array(array('lt',time()),array('NEQ',0),'and'); 
		}
		
		if($search_is_used=='Y'){
			$where['check_time']=array('gt',0);
		}
		if($search_is_used=='N'){
			//$where['check_time']=array('elt',0);
			$where['check_time'] = array(array('EXP',' is null '),array('elt',0),'or'); //id >= 10 or id <=3
			//$where['check_time'] = array('EXP','11');
		}
		$order='ctime desc';
		$pagedata = M('Coupon')->kPageQuery($where,$order,20);
		//echo M('Coupon')->getLastSql();
		$this->assign('pagedata', $pagedata);
		unset($_REQUEST['__hash__']);
		unset($_REQUEST['pno']);
		unset($_REQUEST['goPageFun']);
		$this->assign($_REQUEST);
		$this->display();
	}

	public function ajxCouponDetail(){
		$passcode = $_REQUEST['codevalue'];
		if(empty($passcode))exit(json_encode(array('status'=>-1,'msg'=>'请录入商品券号码！')));
		$result = service('Shanghu')->checkPassCode($passcode,$checkerId);
		exit(json_encode($result));
	}
	
	/**
	 * 测试用。设置礼券已使用
	 */
	public function ajxCouponUsed(){
		exit;
		$passcode = $_POST['codevalue'];
		$checker_id = intval($_POST['checker_id']);
		Log::record('ajxCouponUsed 请求参数$passcode='.$passcode.'  $checker_id='.$checker_id,Log::DEBUG,Log::FILE);
		Log::save(); 
		if(empty($passcode))exit(json_encode(array('status'=>-1,'msg'=>'请录入商品券号码！')));
		$result = service('Shanghu')->doConfirmCoupon($passcode,$checker_id);
		exit(json_encode($result));
	}
	
	//删除用户
    public function doClean() {
    	if(!(C('ADMIN_OPEN_ORDER_CLEAN'))){echo 0; exit;}
    	
    	$_POST['oid']	= explode(',', t($_POST['oid']));
		if (empty($_POST['oid'])) {
			echo 0;
			exit;
		}
    	$res = model('Order')->cleanOrder($_POST['oid']);
    	if($res) {echo 1;}
    	else 	 {echo 0;}
    	exit;
    }
    
    
	//设置订单失效，不可用。
    public function doDelete() {
    	$_POST['oid']	= explode(',', t($_POST['oid']));
		if (empty($_POST['oid'])) {
			echo 0;
			exit;
		}
    	$res = model('Order')->deleteOrder($_POST['oid'],$_POST['remark']);
    	if($res) {echo 1;}
    	else 	 {echo 0;}
    	exit;
    }
}
?>